# -*- coding: UTF-8 -*-
"""
    翻译
    @Project ：auto_translate_voice
    @File ：translate_config_text.py
    @Author ：lc
    @Date ：2022.4.29 15:37
"""
import os
import time

from selenium import webdriver
from selenium.webdriver.common.by import By

from config import trim_url, record_audio_path, trim_save_path, file_prefix, last_trim_save_path, translate_url
from utils import first_trim, trim, save_file, delete_domain_name, get_all_text, save_all_translate_text

webdriver_obj = webdriver.Chrome()
webdriver_obj.maximize_window()
webdriver_obj.get(translate_url)
time.sleep(5)
webdriver_obj.find_element(By.XPATH,
                           r"//section[@aria-labelledby='translation-source-heading']//span[@class='lmt__language_selec"
                           r"t__active__title']/strong[text()='检测源语言']").click()
time.sleep(2)
webdriver_obj.find_element(By.XPATH, "//button[@dl-test='translator-lang-option-zh']").click()
time.sleep(2)
translate_text_list = list()
for text_item in get_all_text():
    if text_item is not None:
        text_area = webdriver_obj.find_element(By.XPATH,
                                               "//section[@aria-labelledby='translation-source-heading']//textarea")
        print("获取到的所有文本：",text_item)
        text_area.send_keys(text_item)
        time.sleep(8)
        result = webdriver_obj.find_element(By.XPATH, "//div[@id='target-dummydiv']")
        print("获取到的翻译文本", result.get_attribute("textContent").strip().replace('\n', '').replace('\r', ''),
              "文本长度：", len(result.get_attribute("textContent")))
        translate_text_list.append(result.get_attribute("textContent").strip().replace('\n', '').replace('\r', ''))
        print("获取文本结束...")
        text_area.clear()
        print('循环中的翻译列表：', translate_text_list)
file = open("zan_cun.text", "w", encoding="utf-8")
file.writelines(translate_text_list)
print("开始存储...")
save_all_translate_text(translate_text_list)
